String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
};
sdApp.controller("sdHeaderCtrl", function($rootScope, $scope, $timeout, $http, transformRequestAsFormPost, $state, APPService) {
    $scope.credentials = {};
    $scope.registration = {};

    $scope.attemptSignup = function() {

        $scope.confirm = {};
        $scope.error = {};

        $scope.validate = function() {
            var isValid = true;
            $scope.error = {};
            for (f in $scope.registration) {
                var fval = $scope.registration[f];
                if (!fval) {
                    if (f != 'passwordRepeat') {
                        $scope.error[f] = f.capitalize() + ' required';
                        isValid = false;
                    }
                } else {
                    switch (f) {
                        case 'email':
                            if (!(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(fval))) {
                                $scope.error[f] = 'Enter valid email';
                                isValid = false;
                            }
                            break;
                        case 'password':
                            if (fval.length < 6) {
                                $scope.error[f] = 'Password requires atleast 6 characters';
                                isValid = false;
                            } else if ($scope.registration['passwordRepeat'] != fval) {
                                $scope.error['passwordRepeat'] = 'Password doesnot match';
                                isValid = false;
                            }
                            break;
                    }
                }
            }
            if (!$scope.confirm.age)
                $scope.error.confirm_age = 'Please confirm your age';
            if (!$scope.confirm.terms)
                $scope.error.confirm_terms = 'Please read & accept our tems and condition';
            return isValid;
        };
        /*if (!$scope.registration.firstName || !$scope.registration.firstName.length
                || !$scope.registration.lastName || !$scope.registration.lastName.length
                || !$scope.registration.password || !$scope.registration.password.length
                || !$scope.registration.email || !$scope.registration.email.length) {
            $scope.registration.errorMessage = "All fields are required!";
            return false;
        }

        if ($scope.registration.email.indexOf('@') < 0) {
            $scope.registration.errorMessage = "Please enter a valid email id";
            return false;
        }

        if (!$scope.registration.password || !$scope.registration.password.length > 5 || $scope.registration.password !== $scope.registration.passwordRepeat) {
            $scope.registration.errorMessage = "Passwords should match and be atleast 6 characters";
            return false;
        }*/
        if ($scope.validate()) {
            // Store the data-dump of the FORM scope.
            var postData = {
                firstname: $scope.registration.firstName,
                lastname: $scope.registration.lastName,
                password: $scope.registration.password,
                login: $scope.registration.email
            };
            APPService.registeerUser(postData).then(
                function(data) {
                    if (data.result == "success" && data.id > -1) {
                        $rootScope.stateCard.userId = data.id;
                        $rootScope.stateCard.email = data.email;
                        $rootScope.stateCard.firstName = data.firstName;
                        $rootScope.stateCard.lastName = data.lastName;
                        $('#registerModal').modal('hide');
                    } else {
						$scope.error.signup = data.message;
					}
                }
            );
        }
    };

    $scope.reset = function() {
        $scope.error = {};
        for (f in $scope.registration) {
            $scope.registration[f] = '';
        }
        $scope.credentials.username = '';
        $scope.credentials.password = '';
    };

    $scope.attemptLogin = function() {

        $scope.error = {};
        var isValid = true;
        if (!$scope.credentials.username) {
            $scope.error.username = 'Username required';
            isValid = false;
        }
        if (!$scope.credentials.password) {
            $scope.error.password = 'Password required';
            isValid = false;
        }

        if (isValid) {
            // Store the data-dump of the FORM scope.
            var postData = {
                usr: $scope.credentials.username,
                pwd: $scope.credentials.password
            };
            APPService.authenticate(postData).then(
                function(data) {
                    if (data.result == "passed" && data.userid > -1) {
                        $rootScope.stateCard.userId = data.userid;
                        $rootScope.stateCard.email = data.email;
                        $rootScope.stateCard.firstName = data.firstName;
                        $rootScope.stateCard.lastName = data.lastName;

                        $('#loginModal').modal('hide');
                        $('div.modal-backdrop').remove();
                        $state.go('dashboard');
                    } else {
                        $scope.error.login = "Invalid Username or Password"
                    }
                }
            );
        }
    };

    $scope.attemptLogout = function() {
        APPService.logout();
    }
});